// XmlFileView.cpp : implementation file
//

#include "stdafx.h"
#include "UIEditor_CPP.h"
#include "XmlFileView.h"
#include "window_project.h"
#include "error_list.h"

// CXmlFileView

IMPLEMENT_DYNCREATE(CXmlFileView, CFormView)

CXmlFileView::CXmlFileView()
	: CFormView(CXmlFileView::IDD)
{

}

CXmlFileView::~CXmlFileView()
{
}

void CXmlFileView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TAB_XML, m_Tab);
}

BEGIN_MESSAGE_MAP(CXmlFileView, CFormView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_XML, &CXmlFileView::OnTcnSelchangeTabXml)
	ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()


// CXmlFileView diagnostics

#ifdef _DEBUG
void CXmlFileView::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CXmlFileView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CXmlFileView message handlers


int CXmlFileView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	CRect rt;
	rt.SetRectEmpty();

	//if (!m_newTab.Create(CMFCTabCtrl::STYLE_FLAT, rt, this, 1))
	//{
	//	TRACE0("Failed to create output tab window\n");
	//	return -1;      // fail to create
	//}

	m_Tab.Create(WS_CHILD | WS_VISIBLE | WS_THICKFRAME | TCS_RIGHT | TCS_VERTICAL, rt, this, IDC_TAB_XML);
	m_Tab.InsertItem(0, L"references");
	m_Tab.InsertItem(1, L"controls");

	m_panReference.Create(CPanelReference::IDD, this);
	m_panControl.Create(CPanelControl::IDD, this);
	
	ShowTab(1);
	m_Tab.SetCurSel(1);
	return 0;
}


void CXmlFileView::OnSize(UINT nType, int cx, int cy)
{
	CFormView::OnSize(nType, cx, cy);
	
	CRect parentRect;
	GetClientRect(parentRect);

	m_Tab.MoveWindow(0, 0, cx, cy);
	CRect rt;
	m_Tab.GetClientRect(&rt);

	//m_newTab.MoveWindow(0, 0, parentRect.Width()-30, parentRect.Height());
	m_panReference.MoveWindow(0, 0, parentRect.Width()-30, parentRect.Height());
	m_panControl.MoveWindow(0, 0, parentRect.Width()-30, parentRect.Height());
}

void CXmlFileView::ShowTab(int aIndex)
{
	m_panReference.ShowWindow(aIndex == 0?SW_SHOW:SW_HIDE);
	m_panControl.ShowWindow(aIndex == 1?SW_SHOW:SW_HIDE);
}

void CXmlFileView::OnTcnSelchangeTabXml(NMHDR *pNMHDR, LRESULT *pResult)
{
	int iIndex = m_Tab.GetCurSel();
	switch (iIndex)
	{
	case -1:
		{
			break;
		}
	case 0:
		{
			m_panReference.SetFocus();
			ShowTab(iIndex);
			break;
		}
	case 1:
		{
			m_panControl.SetFocus();
			ShowTab(iIndex);
			break;
		}
	}
	*pResult = 0;
}

void CXmlFileView::on_event(s_data_event& e)
{
	switch(e.m_event_type)
	{
	case EDO_ENTER:
		{
			m_data = e.m_sender;
			m_panControl.m_dummyXmlData = (data_xml*)e.m_sender;
		}
	case EDO_DATA_CHANGE:
		{
			int cur = m_Tab.GetCurSel();
			switch(cur)
			{
			case 0:
				m_panReference.Invalidate();
				break;
			case 1:
				m_panControl.Invalidate();
				break;
			default:
				break;
			}
		}
		break;
	case EDO_CTL_TREE_CHANGED:
	case EDO_XML_SEL_CHANGED:
		{
			m_panControl.m_dummyXmlData->m_window.set_cur_select_ctl((control*)e.m_arg);
			m_panControl.Invalidate();
		}
	case EDO_LEAVE:
		{
			
		}
		break;
	}
}

void CXmlFileView::on_set_data(i_data* aData)
{	
	if (aData)
	{
		m_panControl.m_dummyXmlData = (data_xml*)aData;
		m_panControl.m_dummyXmlData->m_window.m_controlTree = ((data_xml*)aData)->m_controls.m_controlTree;
	}	
}
LRESULT CXmlFileView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	// TODO: Add your specialized code here and/or call the base class
	
	return __super::WindowProc(message, wParam, lParam);
}


void CXmlFileView::OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/)
{
	// TODO: Add your message handler code here

}

BOOL CXmlFileView::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	switch(pMsg->message)
	{
	case WM_KEYDOWN:
	case WM_KEYUP:
		m_panControl.WindowProc(pMsg->message, pMsg->wParam, pMsg->lParam);
		return TRUE;
	}
	return __super::PreTranslateMessage(pMsg);
}
